package com.cly.config;

import com.cly.handler.AuthHandshakeInterceptor;
import com.cly.handler.WebSocketHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * WebSocket配置类
 *
 * @author by 春风能解释
 * <p>
 * 2025/1/22
 */
@Configuration
@EnableWebSocket
@RequiredArgsConstructor
//https://liuyueyi.github.io/tutorial/spring/web/WebSocket/
public class WebSocketConfiguration implements WebSocketConfigurer {

    /**
     * WebSocketHandler
     */
    private final WebSocketHandler webSocketHandler;

    /**
     * 认证拦截器
     */
    private final AuthHandshakeInterceptor authHandshakeInterceptor;

    /**
     * 注册WebSocketHandler
     *
     * @param registry 注册器
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler, "/websocket/message")// 注册WebSocketHandler
                .setAllowedOrigins("*")// 允许跨域
                .addInterceptors(authHandshakeInterceptor);// 添加认证拦截器
    }
}
